﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WF_TheTourManagement.EntityClass;

namespace WF_TheTourManagement
{
    public partial class LocationManament : Form
    {
        
        public LocationManament()
        {
            InitializeComponent();
        }

        DataTable dt;
        Location l;

        void bindLocationList(DataGridView dvg, DataTable dt)
        {
            dvg.DataSource = null;
            dvg.DataSource = dt;

            

        }

        public void loadLocationList()
        {
            l = new Location();
            dt = l.getLocationsList();

            dgvLocationList.DataSource = dt;
        }

        void bindData()
        {
            txtLocationId.DataBindings.Clear();
            txtLocationId.DataBindings.Add("Text", dgvLocationList.DataSource, "LocationId");
            txtLocationName.DataBindings.Clear();
            txtLocationName.DataBindings.Add("Text", dgvLocationList.DataSource, "LocationName");
            txtLocationDescription.DataBindings.Clear();
            txtLocationDescription.DataBindings.Add("Text", dgvLocationList.DataSource, "LocationDescription");
        }

        private void LocationManament_Load(object sender, EventArgs e)
        {
            txtSearch.Focus();
            lbGuide.Visible = false;
            //btnSave.Enabled = false;
            txtLocationId.Enabled = false;
            loadLocationList();
            bindData();
                
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //lbGuide.Visible = true;
            //lbGuide.Text = "Enter the above Two fields, then click \"Save\".";
            btnSave.Enabled = true;
            txtLocationId.Clear();
            txtLocationId.Enabled = false;
            txtLocationName.Clear();
            txtLocationDescription.Clear();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
            if (txtLocationId.Text == "")
            {
                string locationName = txtLocationName.Text;
                string locationDescription = txtLocationDescription.Text;
                Location _location = new Location(locationName, locationDescription);
                //MessageBox.Show("Added");                 
                _location.saveLocation(dt);
                
            }

            else
            {
                int locationId = Convert.ToInt32(txtLocationId.Text);
                string locationName = txtLocationName.Text;
                string locationDescription = txtLocationDescription.Text;
                Location _location = new Location(locationId, locationName, locationDescription);
                _location.updateLocation(dt);
            }                        
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int locationId = Convert.ToInt32(txtLocationId.Text);
            string locationName = txtLocationName.Text;
            string locationDescription = txtLocationDescription.Text;
            Location _location = new Location(locationId, locationName, locationDescription);
            _location.deleteLocation(dt);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            
            TourManagement f = new TourManagement();
            f.Show();
            this.Close();
            this.Dispose();
        }
    }
}
